#!/usr/bin/perl

barcode
current_center
sampling_center
rsid
gender
specimen_type
visit_num
volume
ctr_volume
ng_per_ul
in_library_construction
ii_seq_june_2012

while(<STDIN>) {
  s/\n//;


}

# first row: 
print "\tSamples\t";
for(my $i = 0; $i < $test->{'count'}; $i++) {
    print "$test->{$i}->{'name'}\t";
}
print "\n";

# second row
my $n = scalar(@IDs);

print "Totals:\t$n\t";

for(my $i = 0; $i < $test->{'count'}; $i++) {
    my $count = 0;
    foreach my $srs (@IDs) {
	my @l = @{$test->{$i}->{'test'}};
	$count++ if (item_exists($srs, $test_list, @l ));
    }
    print "$count\t";
}

print "\n";

# skip a row
print "\t\t";
for(my $i = 0; $i < $test->{'count'}; $i++) {
    print "\t";
}
print "\n";

# next header

print "SRS_ID\tCount\tTEST: count > $cutoff\t";
for(my $i = 0; $i < $test->{'count'}; $i++) {
    print "$test->{$i}->{'name'}\t";
}
print "\n";


# main body. 
foreach my $srs (@IDs) {

    my $count = get_good_tests($srs, $test, $test_list);
    printf("%s\t%d\t%s\t",
	   $srs,
	   $count,
	   ($count > $cutoff) ? "PASS" : "FAIL");

    for(my $i = 0; $i < $test->{'count'}; $i++) {

	my @l = @{$test->{$i}->{'test'}};
	
	if (item_exists($srs, $test_list, @l )) {
	    print "YES\t";
	}
	else {
	    print "NO\t";
	}
    }
    print "\n";
}

sub get_good_tests {
    my $srs = shift;
    my $test = shift;
    my $test_list = shift;

    my $count = 0;

    for(my $i = 0; $i < $test->{'count'}; $i++) {
	
	my @l = @{$test->{$i}->{'test'}};
	
	$count++ if (item_exists($srs, $test_list, @l ));

    }

    return($count);
}

sub item_exists {
    my ($srs, $test_list, @l) = @_;
    my $r = 0;

    foreach my $str (@l) {
	$str =~ s/SRS_KEY/$srs/g;
	$r = 1 if ($test_list->{$str} == 1);
    }

    return($r);
}

sub fill_cell {
    my $x = shift;

    my $r = "no";
    $r = "yes" if ($x == 1);

    return($r);
}

sub load_tests {
    my $test;

    $test->{0}->{'name'} = "Screened - MD5";
    $test->{0}->{'test'} = 
	["02-ScreenedReads/ProcessedForAssembly/SRS_KEY/SRS_KEY.md5"];


    $test->{1}-> {'name'} = "Screened - trimmed";
    $test->{1}-> {'test'} = 
	["02-ScreenedReads/ProcessedForAssembly/SRS_KEY/SRS_KEY.denovo_duplicates_marked.trimmed.singleton.fastq.bz2"];

    $test->{2}-> {'name'} = "Assembled PGA";
    $test->{2}-> {'test'} =
	["03-Assembly/PGA/SRS_KEY_WUGC/SRS_KEY_WUGC.scaffolds.fa",
	 "03-Assembly/PGA/SRS_KEY_LANL/SRS_KEY_LANL.scaffolds.fa",
	 "03-Assembly/PGA/SRS_KEY_Baylor/SRS_KEY_Baylor.scaffolds.fa"];


    $test->{3}-> {'name'} = "Annotated ORFs fasta";
    $test->{3}-> {'test'} = 
	["04-Annotation/ReadAnnotationProteinDBS/MetageneScaffolds/SRS_KEY_Scaffolds.metagenemark.faa.gz"];

    $test->{4}-> {'name'} = "Annotated ORFs gff";
    $test->{4}-> {'test'} = 
	["04-Annotation/ReadAnnotationProteinDBS/MetageneScaffolds/SRS_KEY_Scaffolds.metagenemark.faa.gz"];

    $test->{5}-> {'name'} = "Mapped to KEGG";
    $test->{5}-> {'test'} = 
	["05-Analysis/ProteinDBsMappingResults/KEGG/EnzymeAbundances/SRS_KEY_vs_KEGG_v54__01-keg.txt"];


    $test->{6}-> {'name'} = "Mapped to Reference";
    $test->{6}-> {'test'} = 
	["05-Analysis/ReadMappingToReference/SRS_KEY/SRS_KEY_abundance_table.tsv"];


    $test->{7}-> {'name'} = "Mapped to Assembly";
    $test->{7}-> {'test'} = 
	["05-Analysis/ReadMappingToAssembly/SRS_KEY/SRS_KEY.denovo_duplicates_marked.trimmed.unplaced.singleton.fastq.gz"];


    $test->{'count'} = 8;

    return($test);
}
